<?php

namespace mpend\modules\team\models;

use Yii;
use common\helpers\ArrayHelper;
use common\helpers\Url;
use common\models\workbook\WorkbookRecord;
use common\logics\WorkbookLogic;
use common\components\office\WorkbookExcel;
use common\components\office\grid\SerialColumn;

/**
 * 记工统计表格下载模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class WorkBookStatsDownload extends \common\base\Model {

    /**
     * @var string 记工薄ID
     */
    public $workbook_id;

    /**
     * @var string|date 开始时间
     */
    public $begin_date;

    /**
     * @var string|date 结束时间
     */
    public $end_date;

    /**
     * @var WorkbookLogic|null 技工本信息
     */
    private $_book;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['begin_date', 'end_date'], 'date'],
            ['workbook_id', 'validateWorkbook'],
        ];
    }

    /**
     * 验证记工薄信息
     * @param string $attribute Description
     */
    public function validateWorkbook($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $workbook = WorkbookLogic::findByManageControl($this->$attribute);
        if (!$workbook) {
            return $this->addError($attribute, '记工薄信息不存在');
        }
        $this->_book = $workbook;
    }

    /**
     * @return WorkbookLogic
     */
    public function getBook() {
        return $this->_book;
    }

    /**
     * 生成下载文件的名称
     * @return string
     */
    public function getTitle() {
        $book = $this->getBook();
        return Yii::t('message', '{begin}-{end}{title}{name}', [
            'begin' => $this->begin_date,
            'end' => $this->end_date,
            'title' => $book->title,
            'name' => '记工统计表',
        ]);
    }

    /**
     * 生成下载文件的名称
     * @return string
     */
    public function getFileName($ext = true) {
        $book = $this->getBook();
        return Yii::t('message', '{begin}-{end}{title}{name}{ext}', [
            'begin' => date('Y.m.d', strtotime($this->begin_date)),
            'end' => date('Y.m.d', strtotime($this->end_date)),
            'title' => $book->title,
            'name' => '记工统计表',
            'ext' => $ext ? '.xlsx' : '',
        ]);
    }

    /**
     * 生成下载文件的名称
     * @return string
     */
    public function getSaveFileName() {
        return md5(implode('', $this->attributes));
    }

    /**
     * 记工日期
     * @return string
     */
    public function download() {
        if (!$this->validate()) {
            return $this->dumpError('');
        }
//        $data = $this->loadData();
        $excel = new WorkbookExcel([
            'dataProvider' => $this->searchDataProvider(),
            'title' => $this->getTitle(),
            'filename' => $this->getSaveFileName(),
            'captions' => $this->getCaptionOptions(),
            'dataProvider' => $this->searchDataProvider(),
            'columns' => [
                [
                    'class' => SerialColumn::className(),
                    'header' => '序号',
                    'footer' => '合计：',
                    'headerOptions' => [
                        'alignment' => [
                            'horizontal' => 'center',
                        ],
                        'font' => [
                            'bold' => true,
                        ],
                    ],
                    'contentOptions' => [
                        'alignment' => [
                            'horizontal' => 'center',
                        ],
                    ],
                    'footerOptions' => [
                        'alignment' => [
                            'horizontal' => 'center',
                        ],
                    ],
                ],
                [
                    'attribute' => 'realname',
                    'headerOptions' => [
                        'width' => 10,
                        'font' => [
                            'bold' => true,
                        ],
                    ],
                    'contentOptions' => [
                        'quotePrefix' => true
                    ],
                ],
                [
                    'attribute' => 'workloads',
                    'headerOptions' => [
                        'width' => 12,
                        'alignment' => [
                            'horizontal' => 'right',
                        ],
                        'font' => [
                            'bold' => true,
                        ],
                    ],
                ],
                [
                    'attribute' => 'workAmount',
                    'headerOptions' => [
                        'width' => 12,
                        'alignment' => [
                            'horizontal' => 'right',
                        ],
                        'font' => [
                            'bold' => true,
                        ],
                    ],
                ],
                [
                    'attribute' => 'loan',
                    'headerOptions' => [
                        'width' => 12,
                        'alignment' => [
                            'horizontal' => 'right',
                        ],
                        'font' => [
                            'bold' => true,
                        ],
                    ],
                ],
                [
                    'attribute' => 'balance',
                    'headerOptions' => [
                        'width' => 14,
                        'alignment' => [
                            'horizontal' => 'right',
                        ],
                        'font' => [
                            'bold' => true,
                        ],
                    ],
                ],
                [
                    'attribute' => 'loanAmount',
                    'headerOptions' => [
                        'width' => 28,
                        'alignment' => [
                            'horizontal' => 'center',
                            'wrapText' => true,
                        ],
                        'font' => [
                            'bold' => true,
                        ],
                    ],
                ],
                [
                    'attribute' => 'balanceAmount',
                    'headerOptions' => [
                        'width' => 28,
                        'alignment' => [
                            'horizontal' => 'center',
                            'wrapText' => true,
                        ],
                        'font' => [
                            'bold' => true,
                        ],
                    ],
                ],
            ],
            'totalColumns' => [
                'workloads',
                'workAmount',
                'loan',
                'balance',
                'loanAmount',
                'balanceAmount',
            ],
        ]);
        $filePath = $excel->export();
        return [
            'icon' => Url::attach('/assets/icons/file/excel.png'),
            'md5' => $this->getSaveFileName(),
            'name' => $this->getFileName(),
            'path' => Url::to($filePath, true),
        ];
    }

    public function getCaptionOptions() {
        return [
            [
                'text' => '报表生成时间：' . date('Y-m-d H:i:s'),
                'styles' => [
                    'alignment' => [
                        'horizontal' => 'center',
                        'vertical' => 'center',
                        'wrapText' => true,
                    ],
                ],
            ],
            [
                'text' => '班组长/工头：' . $this->getBook()->headman,
                'styles' => [
                    'alignment' => [
                        'horizontal' => 'center',
                        'vertical' => 'center',
                        'wrapText' => true,
                    ],
                ],
            ],
            [
                'text' => '相关说明：借支和已结工钱均为统计时间段内的总和，统计时间段外不计算入内',
                'styles' => [
                    'font' => [
                        'color' => [
                            'rgb' => 'ff6600'
                        ]
                    ],
                    'alignment' => [
                        'horizontal' => 'center',
                        'vertical' => 'center',
                        'wrapText' => true,
                    ],
                ],
            ],
        ];
    }

    public function searchDataProvider() {
        $model = new WorkBookStatsMemberSearch();
//        $model->begindate = $this->begin_date;
//        $model->enddate = $this->end_date;
        $model::$begindate = $this->begin_date;
        $model::$enddate = $this->end_date;
        $model->workbook_id = $this->workbook_id;
        Yii::error($model->attributes);
        $model->validate();
        return new \yii\data\ActiveDataProvider([
            'query' => $model->buildQuery(),
            'pagination' => false
        ]);
    }

    public function loadData() {
        $model = new WorkBookStatsMemberSearch();
        $model->begindate = $this->begin_date;
        $model->enddate = $this->end_date;
        $model->workbook_id = $this->workbook_id;
        $model->pagesize = false;
        return $model->search();
    }

}
